#include "TPCidle.h"
#include "TPCwalking.h"
#include "GameObject.h"
#include "ThirdPersonController.h"
#include "Animation.h"
#include "Logger.h"
#include "Exception.h"

using namespace game_engine;
using namespace game_engine::library;
using namespace game_engine::components;
using namespace game_engine::utils;

///////////////////////////////////////////////////////////////////////////////
// Constructors and Destructors
///////////////////////////////////////////////////////////////////////////////

TPCidle::TPCidle(void)
    : State()
{
}

TPCidle::TPCidle(Behaviour *parent)
    : State(parent)
{
}

TPCidle::~TPCidle(void)
{
}

///////////////////////////////////////////////////////////////////////////////
// Other public methods
///////////////////////////////////////////////////////////////////////////////

void TPCidle::onEnter(void) throw(...)
{
    // Reset sequence time
    Animation *anim;
    try
    {
        anim = dynamic_cast<Animation*> 
            ((fsm->getObject())->getComponent("Animation"));
    } catch (...)
    {
        string msg("[TPCidle::onEnter] animation component not found");
        Logger::ERR(GAMEENGINE_LOG, msg);
        throw RuntimeException(msg);
    }
    anim->resetSequence();
    anim->setActiveSequence(
        dynamic_cast<ThirdPersonController*> (fsm)->getIdleAnimationId());
        
    nextState = this;
}

void TPCidle::onLoop(const FLOAT32 time) throw(...)
{
    // Check if user wants to move
    if (fsm->keyPressed(GLUT_KEY_RIGHT) || fsm->keyPressed(GLUT_KEY_LEFT) || 
        fsm->keyPressed(GLUT_KEY_UP) || fsm->keyPressed(GLUT_KEY_DOWN))
    {
        nextState = new TPCwalking(fsm);
    }
}
